
package com.wyzz.hr.bo.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IEmpTransferBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.dao.IEmpTransferDao;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Emptransfer;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.Pager;


public class EmpTransferBoImpl implements IEmpTransferBo {

	public EmpTransferBoImpl() {
	}

	public List findTransferByEmpNo(String empNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptransfer.class);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EMPLOYEE,
				FetchMode.DEFAULT);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EFT_NEW_DEPT_NO,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EFT_OLD_DEPT_NO,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EFT_OLD_PB_NO,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EFT_NEW_PB_NO,
				FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
				Emptransfer.PROP_EMPLOYEE).append(".").append(Employee.PROP_ID)
				.toString(), empNo));
		detachedCriteria.addOrder(Order.desc(Emptransfer.PROP_EFT_CREATE_DATE));
		List result = empTransferDao.findByCriteria(detachedCriteria);
		return result;
	}

	public List findTransfer(DetachedCriteria dc, Pager page,
			String searchOrExport) {
		if (page == null || "export".equals(searchOrExport)) {
			return empTransferDao.findByCriteria(dc);
		} else {
			page.splitPage(dc);
			return empTransferDao.findByCriteria(dc, page.getPageSize(), page
					.getCurrentPage());
		}
	}

	public Emptransfer getById(String transferId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptransfer.class);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EMPLOYEE,
				FetchMode.DEFAULT);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EFT_NEW_DEPT_NO,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Emptransfer.PROP_EFT_OLD_DEPT_NO,
				FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq(Emptransfer.PROP_EFT_ID,
				transferId));
		List result = empTransferDao.findByCriteria(detachedCriteria);
		if (result.size() == 0)
			return null;
		else
			return (Emptransfer) result.get(0);
	}

	public void delete(String etfId, Employee currentEmp) {
		Emptransfer emptransfer = getById(etfId);
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		Employee employee = employeeBo.loadEmp(emptransfer.getEmployee()
				.getId(), null);
		employee.setEmpDeptNo(new Department(emptransfer.getEftOldDeptNo()
				.getId()));
		employee.setEmpPbNo(new PositionBase(emptransfer.getEftOldPbId()
				.getId()));
		employee.setEmpLastChangeBy(currentEmp);
		employee.setEmpLastChangeTime(new Date());
		empTransferDao.updateObject(employee);
		String hql = (new StringBuilder()).append(
				"delete from Emptransfer where eftId='").append(etfId).append(
				"'").toString();
		empTransferDao.exeHql(hql);
	}

	public void update(Emptransfer newEmpTransfer, String transferId) {
		Emptransfer emptransfer = getById(transferId);
		emptransfer.setEftTransferDate(newEmpTransfer.getEftTransferDate());
		emptransfer.setEftTransferType(newEmpTransfer.getEftTransferType());
		emptransfer.setEftReason(newEmpTransfer.getEftReason());
		emptransfer.setEftComments(newEmpTransfer.getEftComments());
		emptransfer.setEmployee(newEmpTransfer.getEmployee());
		emptransfer.setEftNewDeptNo(newEmpTransfer.getEftNewDeptNo());
		emptransfer.setEftLastChangeBy(newEmpTransfer.getEftLastChangeBy());
		emptransfer.setEftLastChangeTime(newEmpTransfer.getEftLastChangeTime());
		empTransferDao.saveOrupdate(emptransfer);
	}

	public List getEftByEmp(Employee emp) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptransfer.class);
		dc.createAlias(Emptransfer.PROP_EMPLOYEE, "emp", 1);
		dc.add(Restrictions.eq(Emptransfer.PROP_EMPLOYEE, emp));
		dc.addOrder(Order.asc(Emptransfer.PROP_EFT_TRANSFER_DATE));
		return empTransferDao.findByCriteria(dc);
	}

	public boolean saveImportList(List transList, List empList, List posList,
			List emphistorgList) {
		if (transList.size() == 0) {
			return true;
		} else {
			empTransferDao.saveOrupdate(transList);
			empTransferDao.saveOrupdate(empList);
			empTransferDao.saveOrupdate(posList);
			empTransferDao.saveOrupdate(emphistorgList);
			return true;
		}
	}

	public IEmpTransferDao getEmpTransferDao() {
		return empTransferDao;
	}

	public void setEmpTransferDao(IEmpTransferDao empTransferDao) {
		this.empTransferDao = empTransferDao;
	}

	IEmpTransferDao empTransferDao;
}
